package com.shbaiche.bifenba.ui.data;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.TextView;

import com.sevenheaven.segmentcontrol.SegmentControl;
import com.shbaiche.bifenba.R;
import com.shbaiche.bifenba.model.Event;
import com.shbaiche.bifenba.model.data.CompeteVo;
import com.shbaiche.bifenba.ui.base.BaseFragment;
import com.shbaiche.bifenba.ui.base.XStateFragmentAdapter;
import com.shbaiche.bifenba.ui.home.CategoryTabStrip;
import com.shbaiche.bifenba.ui.my.FollowTeamFragment;
import com.shbaiche.bifenba.ui.my.MyCommentListActivity;
import com.shbaiche.bifenba.view.WheelView;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroid.event.BusFactory;
import cn.droidlover.xdroidbase.base.XFragmentAdapter;
import me.shaohui.bottomdialog.BottomDialog;

/**
 * 数据首页
 */

@SuppressLint("ValidFragment")
public class DataContentFragment extends BaseFragment {

    @BindView(R.id.segment_control)
    SegmentControl segmentControl;

    @BindView(R.id.view_pager_content)
    ViewPager pager;

    @BindView(R.id.tv_year)
    TextView tvYear;



    List<Fragment> fragmentList = new ArrayList<>();
    XStateFragmentAdapter adapter;

    CompeteVo mCompeteVo;

    BottomDialog bottomDialog;

    private String year;

    Bundle bundle = new Bundle();


    public DataContentFragment(){

    }

    public static DataContentFragment newInstance() {
        return new DataContentFragment();
    }


    @Override
    public void initData(Bundle savedInstanceState) {

        if(savedInstanceState!=null && savedInstanceState.containsKey("data")){
            mCompeteVo = (CompeteVo)savedInstanceState.getSerializable("data");
        }else{
            mCompeteVo = (CompeteVo)getArguments().getSerializable("data");
        }
        year = getYears().get(0);

        List<Fragment> fragments = getChildFragmentManager().getFragments();
        if (fragments == null || fragments.isEmpty()) {
            fragmentList.clear();
            bundle.putSerializable("data",mCompeteVo);
            bundle.putString("year",year);

            DataIntegralFragment fragmentIntegral = new DataIntegralFragment();
            fragmentIntegral.setArguments(bundle);
            fragmentList.add(fragmentIntegral);

            DataPlayerFragment fragmentPlayer = new DataPlayerFragment();
            fragmentPlayer.setArguments(bundle);
            fragmentList.add(fragmentPlayer);

            DataTeamFragment fragmentTeam = new DataTeamFragment();
            fragmentTeam.setArguments(bundle);
            fragmentList.add(fragmentTeam);

            DataScheduleFragment fragmentSchedule = new DataScheduleFragment();
            fragmentSchedule.setArguments(bundle);
            fragmentList.add(fragmentSchedule);

        }else{
            fragmentList.clear();
            for (Fragment fragment : fragments) {
                if (fragment instanceof Fragment) {
                    fragmentList.add(fragment);
                }
            }

        }


        if (adapter == null) {
            adapter = new XStateFragmentAdapter(getChildFragmentManager(), fragmentList, null);
        }

        pager.setAdapter(adapter);
        pager.setOffscreenPageLimit(4);

        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                segmentControl.setSelectedIndex(position);
//                if(year.containsKey(position)){
//                    tvYear.setText(year.get(position));
//                }else{
//                    tvYear.setText(getYears().get(0));
//                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        segmentControl.setOnSegmentControlClickListener(new SegmentControl.OnSegmentControlClickListener() {
            @Override
            public void onSegmentControlClick(int index) {
                pager.setCurrentItem(index);
            }
        });

        tvYear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
            }
        });

        tvYear.setText(getYears().get(0));

    }

    public interface OnYearChange{
        public void onChange(String year);
    }

    private  List<String> getYears(){
        List<String> items = new ArrayList<>();
        if(mCompeteVo!=null){
            switch (pager.getCurrentItem()){
                case 0:
                    if(mCompeteVo.getStandingYear()!=null){
                        items.addAll(mCompeteVo.getStandingYear());
                    }
                    break;
                case 1:
                    if(mCompeteVo.getShooterYear()!=null) {
                        items.addAll(mCompeteVo.getShooterYear());
                    }
                    break;
                case 2:
                    if(mCompeteVo.getTeamYear()!=null) {
                        items.addAll(mCompeteVo.getTeamYear());
                    }
                    break;
                case 3:
                    if(mCompeteVo.getMatchesYear()!=null) {
                        items.addAll(mCompeteVo.getMatchesYear());
                    }

                    break;
            }
        }
        if(items.size() == 0){
            int year = new Date(System.currentTimeMillis()).getYear();
            year += 1900;
            items.add(""+year);
            items.add(""+(year-1));
            items.add(""+(year-2));
            items.add(""+(year-3));
            items.add(""+(year-4));
        }

        return items;
    }

    private void showDialog(){
        bottomDialog = BottomDialog.create(getFragmentManager());
        bottomDialog.setLayoutRes(R.layout.layout_dialog_year);
        bottomDialog.setViewListener(new BottomDialog.ViewListener() {
            @Override
            public void bindView(View view) {
                final WheelView wheelView = (WheelView) view.findViewById(R.id.wv);
                List<String> items =  getYears();
                wheelView.setItems(items);
                String y = tvYear.getText().toString();
                if(y!=null){
                    for (int i = 0;i< items.size();i++){
                        if(y.equals(items.get(i))){
                            wheelView.setSeletion(i);
                            break;
                        }
                    }
                }

                TextView tvCancel = (TextView) view.findViewById(R.id.tv_cancel);
                TextView tvOK = (TextView) view.findViewById(R.id.tv_ok);

                tvCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(bottomDialog!=null){
                            bottomDialog.dismiss();
                        }
                    }
                });

                tvOK.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(bottomDialog!=null){
                            bottomDialog.dismiss();
                        }

                        String cur = wheelView.getSeletedItem();

                        tvYear.setText(cur);
                        year = cur;
                        bundle.putString("year",year);

                        BusFactory.getBus().post(new Event.EventYearChange(mCompeteVo,pager.getCurrentItem(),cur));


                    }
                });
            }
        });

        bottomDialog.show();

    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_data_content;
    }


}
